/**


*/


+(function(window) {
        var WS=window.WS=function(option){
            return new WS.fn.init(option);
        };
        WS.fn=WS.prototype={
                init:function(option){
                    this.config={};
                    this.config=this.merge(this.config,option);
                    // this.selectFileForm=this.createDom("<form id='selectFileForm'><input type='file' style='visibility:hidden;width:0px;height:0px;opacity:0;position:absolute;left:-1000px;'></form>");
                    // var fileId=document.getElementById(option.fileId);
                    // that=this;
                    // if(fileId){
                    //     this.listen(fileId,"click",function(){that.selectFile(that)});
                    // }else{
                    //     if(option.fileDom){
                    //         this.listen(option.fileDom,"click",function(){that.selectFile(that)});
                    //     }
                    // }
                    // if(option.handler){
                    //     this.selectFileForm.childNodes[0].onchange=function(){
                    //         option.handler(this.files);
                    //     };
                    // }
                    return this;
                },
				that:this,
                createDom:function(content){
                    var b = document.createElement("div");
                    b.innerHTML = content;
                    content = b.childNodes;
                    return content[0].parentNode.removeChild(content[0]);
                },
                selectFile:function(selector){
                    selector.selectFileForm.childNodes[0] && selector.selectFileForm.childNodes[0].click && selector.selectFileForm.childNodes[0].click();
                },
                clearFiles:function(){
                    this.selectFileForm.reset();
                },
                listen:function(a,b,c){
                    a.addEventListener ? a.addEventListener(b, c, !1) : a.attachEvent ? a.attachEvent("on" + b, c) : a["on" + b] = c;
                },
                filesToFormData:function(files){
                    var fd = new FormData();
                    for(i=0;i<files.length;i++){
                        fd.append(files[i].name,files[i]);
                    }
                    return fd;
                },
                accept:function(fileExtNames){
                    if(Array.isArray(fileExtNames)){
                        fileExtNames.join(",");
                    }
                    this.selectFileForm.childNodes[0].setAttribute("accept", fileExtNames);
                },
                multiple:function(boo){
                    if(boo==true){
                        this.selectFileForm.childNodes[0].setAttribute("multiple", "multiple");
                    }else{
                        this.selectFileForm.childNodes[0].removeAttribute("multiple");
                    }
                },
                merge:function(o1,o2){
                    for(i in o2){
                        o1[i]=o2[i];
                    }
                    return o1;
                },
                readConf:function(key,option,defaultValue){
                    if(!defaultValue){
                        defaultValue=false;
                    }
                    if(option){
                        return option[key]||defaultValue;
                    }
                    return defaultValue;
                },
                execFunc:function(name,data){
                        var arg = Array.prototype.slice.call(arguments, 1);
                        var func=function(){
                            throw 'method #'+name+" not define";
                        };
                        if(this.config[name]){
                            func=this.config[name];
                        }else{
                            if(this[name]){
                                func=this[name];
                            }
                        }
                        return func.apply(this, arg);
                },
                replaceAll:function(str,s1,s2){
                　　return str.replace(new RegExp(s1,"gm"),s2);
                },
				connect:function(connectUrl){
					var that=this;
					that.socket=new WebSocket(connectUrl);
					return that;
				},
				open:function(handler){
					var that=this;
					if(that.socket){
						that.socket.onopen=handler;
						return that;
					}else{
						throw "not connected";
					}
				},
				error:function(handler){
					var that=this;
					if(that.socket){
						that.socket.onerror=handler;
						return that;
					}else{
						throw "not connected";
					}
				},
				close:function(handler){
					var that=this;
					if(that.socket){
						that.socket.onclose=handler;
						return that;
					}else{
						throw "not connected";
					}
				},
				message:function(handler){
					var that=this;
					if(that.socket){
						that.socket.onmessage=handler;
						return that;
					}else{
						throw "not connected";
					}
				},
				send:function(data){
					var that=this;
					if(that.socket){
						that.socket.send(data);
						return that;
					}else{
						throw "not connected";
					}
				},
				ready:function(handler){
					if(this.readystatus){
						handler();
						return;
					}
					var that=this;
					var timer=window.setInterval(function(){
						if(that.socket && that.socket.readyState==1){
							window.clearInterval(timer);
							that.readystatus=true;
							handler();
						}
					},200);
				}
        };
        WS.fn.init.prototype = WS.prototype;
}(window));